package com.mc.moring.widge;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.TextView;

import com.mc.moring.R;


/**
 * 单选 - 一个确认按钮
 */
public class JVipDialog extends Dialog {

    private TextView btnConfirm;
    private TextView btnCancel;
    private TextView title;
    private TextView content;
    private static String mTitle;
    private static String mContent;

    private static JDialogCallback callback;

    public JVipDialog(Context context, JDialogCallback onClickListener) {
        super(context, R.style.JDialog_Them);
        callback = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_jh_vip);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    public void initView() {
        title = findViewById(R.id.tv_title);
        content = findViewById(R.id.tv_content);
        btnConfirm = findViewById(R.id.btn_confirm);
        btnCancel = findViewById(R.id.btn_cancel);
    }

    public void initData() {
        if (!TextUtils.isEmpty(mTitle)) {
            title.setText(mTitle);
        }
        if (!TextUtils.isEmpty(mContent)) {
            content.setText(mContent);
        }
    }

    public void initEvent() {
        btnConfirm.setOnClickListener(v -> {
            callback.confirm(v);
            dismiss();
        });
        btnCancel.setOnClickListener(v -> {
            callback.cancel(v);
            dismiss();
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}

